package com.azxc.rapid.plus.user.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.azxc.rapid.core.log.exception.ServiceException;
import com.azxc.rapid.plus.user.entity.WestoneSectors;
import com.azxc.rapid.plus.user.entity.WestoneSendMsg;
import com.azxc.rapid.plus.user.entity.WestoneStaff;
import com.azxc.rapid.plus.user.mapper.WestoneSectorsMapper;
import com.azxc.rapid.plus.user.mapper.WestoneStaffMapper;
import com.azxc.rapid.plus.user.service.IWestoneSectorsService;
import com.azxc.rapid.plus.user.service.IWestoneSendMsgService;
import com.azxc.rapid.plus.user.service.IWestoneStaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author linym
 * @className: WestoneSectorsServiceImpl
 * @description:
 * @date 2023/1/30 12:39
 * @version：1.0
 */
@Service
public class WestoneStaffServiceImpl extends ServiceImpl<WestoneStaffMapper, WestoneStaff> implements IWestoneStaffService {


	//需要改为配置在nocas
	@Value("${westone.appKey:APPAD3BADB33B0622F1BE4C0E5A}")
	private  String appKey;
	@Value("${westone.corpId:787987360344064}")
	private  String	corpId;

	@Value("${westone.corpSecret:a1024mo37JlrK3u2_MR7yNv8rKXsGD819_sNsrJZ4Ubg0zSi5y_0k568xPRQ7TIe951}")
	private  String  corpSecret;

	@Autowired
	private IWestoneSendMsgService westoneSendMsgService;

	 @Override
	public boolean pushMessageByMobile(List<String> recipients, Integer badge_number, String msgType, String subject, String content, List actions) {
		 String url = "https://cx-service.westonecloud.com/api/apps/v1/access-tokens?app_key=" + appKey + "&corp_id=" + corpId + "&corp_secret=" + corpSecret;
		 String r = HttpUtil.get(url);
		 JSONObject jobject = JSONObject.parseObject(r);
		 String url2 ="https://cx-service.westonecloud.com/api/apps/v1/push?access_token="+jobject.getString("access_token");;
		 JSONObject body =new JSONObject();
		 body.put("type","MOBILE");
		 body.put("recipients",recipients);
		 if(null!=badge_number){
			 body.put("badge_number",badge_number);
		 }
		 JSONObject contentBody =new JSONObject();
		 contentBody.put("msgType",msgType);
		 JSONObject msg =new JSONObject();
		 msg.put("subject",subject);
		 msg.put("content",content);

		 try {
			 ObjectMapper objectMapper=new ObjectMapper();
			 String obj =  objectMapper.writeValueAsString(msg);
 			 contentBody.put("msgContent",obj);
			 body.put("message", objectMapper.writeValueAsString(contentBody));
 		 } catch (IOException e) {
			 log.error("转化出错！调用卫士通推送接口失败！"+e);
			 throw new ServiceException("转化出错！调用卫士通推送接口失败！");
		 }
		 String result=	HttpUtil.post(url2,body.toJSONString());
 		 JSONObject resulet = JSONObject.parseObject(result);
		 WestoneSendMsg msgInfo = new WestoneSendMsg();
		 msgInfo.setContent(body.toJSONString());
		 msgInfo.setRecipients(String.join(",", recipients));
		 msgInfo.setType("MOBILE");
		 msgInfo.setReMsg(result);
		 westoneSendMsgService.save(msgInfo);
		 if(null!=resulet&&resulet.getInteger("err_code")==0){
			 return true;
		 }
		return false;
	}

	@Override
	public boolean pushMessageByACCOUNT(List<String> recipients, Integer badge_number, String msgType, String subject, String content, List actions) {
		String url = "https://cx-service.westonecloud.com/api/apps/v1/access-tokens?app_key=" + appKey + "&corp_id=" + corpId + "&corp_secret=" + corpSecret;
		String r = HttpUtil.get(url);
		JSONObject jobject = JSONObject.parseObject(r);
		String url2 ="https://cx-service.westonecloud.com/api/apps/v1/push?access_token="+jobject.getString("access_token");;
		JSONObject body =new JSONObject();
		body.put("type","ACCOUNT");
		body.put("recipients",recipients);
		if(null!=badge_number){
			body.put("badge_number",badge_number);
		}
		JSONObject contentBody =new JSONObject();
		contentBody.put("msgType",msgType);
		JSONObject msg =new JSONObject();
		msg.put("subject",subject);
		msg.put("content",content);
		ObjectMapper objectMapper=new ObjectMapper();
		String obj= null;
		try {
			obj = objectMapper.writeValueAsString(msg);
			contentBody.put("msgContent",obj);
			body.put("message", objectMapper.writeValueAsString(contentBody));
		} catch (IOException e) {
			log.error("转化出错！调用卫士通推送接口失败！"+e);
			throw new ServiceException("转化出错！调用卫士通推送接口失败！");
		}
		String result=	HttpUtil.post(url2,body.toJSONString());
		JSONObject resulet = JSONObject.parseObject(result);
		WestoneSendMsg msgInfo = new WestoneSendMsg();
		msgInfo.setContent(body.toJSONString());
		msgInfo.setRecipients(String.join(",", recipients));
		msgInfo.setType("MOBILE");
		msgInfo.setReMsg(result);
		westoneSendMsgService.save(msgInfo);
		if(null!=resulet&&resulet.getInteger("err_code")==0){
			return true;
		}
		return false;
	}
}
